package ctu.kfad.run.memory.garbagecollector;

import ctu.kfad.run.InstanceDescriptor;
import ctu.kfad.run.ObjectTable;
import ctu.kfad.run.memory.IStackObjectProvider;
import ctu.kfad.run.stack.AStackObject;
import ctu.kfad.run.stack.ReferenceStackObject;

import java.util.ArrayList;
import java.util.List;

public class SurviveInstanceCollector {

    public List<InstanceDescriptor> collectFromStackFrames(IStackObjectProvider stackObjectProvider, ObjectTable objectTable) {
        final List<InstanceDescriptor> instanceDescriptors = new ArrayList<>();
        final List<AStackObject> aStackObjects = stackObjectProvider.getStackObjects();
        for (AStackObject aStackObject : aStackObjects) {
            if (aStackObject.getType() == AStackObject.REFERENCE_STACK_OBJECT) {
                final int reference = ((ReferenceStackObject) aStackObject).getReference();
                instanceDescriptors.add(objectTable.getDescriptorForAddress(reference));
            }
        }
        return instanceDescriptors;
    }
}
